package com.workcell.workdata.xthings.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.workcell.workdata.common.core.util.R;
import com.workcell.workdata.xthings.service.DataQrCodeService;
import com.workcell.workdata.xthings.service.factory.CodeBaseFactory;
import com.workcell.workdata.xthings.service.factory.HardwareRealDataFactory;
import com.workcell.workdata.xthings.utils.ObjectUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author 廖博文
 * @Date 2022/12/5 13:54
 * @Description
 */
@Service
@RequiredArgsConstructor
public class DataQrCodeServiceImpl implements DataQrCodeService {

	private final CodeBaseFactory codeBaseFactory;
	private final RedisTemplate redisTemplate;
	private final HardwareRealDataFactory hardwareRealDataFactory;
	/** 硬件快照缓存信息*/
	private final String baseCodeBaseRedisKey = "DataQrCode_BaseCodeBaseInfo_%s";
	/** 默认15分钟内真实数据*/
	private final Integer dataRealTime = 15;

	/**
	 * 获取设备的快照信息
	 * @param codeId
	 * @return
	 */
	@Override
	public R getDataSnapshotInfo(String codeId, String hardwareType) {
		// 获取硬件快照信息
		Object baseCodeBaseInfo = redisTemplate.opsForValue().get(String.format(baseCodeBaseRedisKey,codeId));
		if (ObjectUtil.isEmpty(baseCodeBaseInfo)){
			baseCodeBaseInfo = codeBaseFactory.getHardwareInfo(codeId, hardwareType);
			redisTemplate.opsForValue().set(String.format(baseCodeBaseRedisKey,codeId), baseCodeBaseInfo, 1 , TimeUnit.DAYS);
		}
		if (ObjectUtil.isEmpty(baseCodeBaseInfo)){
			return R.failed("查询数据为空");
		}
		// 参数组装工厂
		List<Map<String,Object>> returnList = ObjectUtils.setConditionToListMap(baseCodeBaseInfo);
		// 返回对象
		return R.ok(returnList);
	}

	/**
	 * 获取设备的实时数据【目前控制20分钟】
	 * @param deviceNo
	 * @return
	 */
	@Override
	public R getDataRealTimeInfo(String deviceNo, String hardwareType, String manufacturerCode) {
		// 获取硬件实时信息
		Object hardwareLatelyData = hardwareRealDataFactory.getLatelyDataInfo(deviceNo, hardwareType, manufacturerCode, dataRealTime);
		if (ObjectUtil.isEmpty(hardwareLatelyData)){
			return R.failed("查询数据为空");
		}
		// 参数组装工厂
		List<Map<String,Object>> returnList = ObjectUtils.setConditionToListMap(hardwareLatelyData);
		// 返回对象
		return R.ok(returnList);
	}

	/**
	 * 获取设备的最近六小时折线图数据
	 * @param deviceNo
	 * @return
	 */
	@Override
	public R getDataHourLineChartInfo(String deviceNo, String hardwareType, String manufacturerCode) {
		return R.ok(hardwareRealDataFactory.getDataHourLineChartInfo(deviceNo,hardwareType,manufacturerCode));
	}

	/**
	 * 一物一码-获取设备的实时数据
	 * @param deviceNo
	 * @param hardwareType
	 * @param manufacturerCode
	 * @return
	 */
	@Override
	public R getHardwareDataToXOTOC(String deviceNo, String hardwareType, String manufacturerCode) {
		// 获取硬件实时信息
		Object hardwareLatelyData = hardwareRealDataFactory.getLatelyDataInfo(deviceNo, hardwareType, manufacturerCode, dataRealTime);
		List<Map<String,Object>> returnList = new ArrayList<>();
		if (ObjectUtil.isEmpty(hardwareLatelyData)){
			Map<String, Object> map = new LinkedHashMap<>();
			map.put("name","使用状态");
			map.put("key","usageStatus");
			map.put("value","未使用");
			returnList.add(map);
		} else {
			returnList = ObjectUtils.setConditionToListMap(hardwareLatelyData);
			Map<String, Object> map = new LinkedHashMap<>();
			map.put("name","使用状态");
			map.put("key","usageStatus");
			map.put("value","使用中");
			returnList.add(map);
		}
		return R.ok(returnList);
	}

	@Override
	public Object getRealData(String deviceNo, String hardwareType) {
		return hardwareRealDataFactory.getRealData(deviceNo,hardwareType);
	}

}
